package com.qf.android.huihui.adapter.information;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qf.android.huihui.R;
import com.qf.android.huihui.bean.DetailListItemBean;
import com.qf.android.huihui.view.activity.WebviewActivity;
import com.squareup.picasso.Picasso;

/**
 * Created by lx on 2017/1/11.
 */

public class DetilListItemAdapter extends BaseAdapter {
    private DetailListItemBean bean;
    private Context context;
    private ViewHolder holder;
    private boolean flag=false;
    public DetilListItemAdapter(DetailListItemBean bean, Context context) {
        this.bean = bean;
        this.context = context;
    }

    @Override
    public int getCount() {
        return bean.getData().getItems().size();
    }

    @Override
    public Object getItem(int i) {
        return bean.getData().getItems().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final int n=i;
        holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_detail_item, viewGroup, false);
            holder = new ViewHolder();
            holder.iv = (ImageView) view.findViewById(R.id.detail_item_item_iv);
            holder.tv = (TextView) view.findViewById(R.id.detail_item_item_tv);
            holder.price = (TextView) view.findViewById(R.id.item_item_price);
            holder.address = (TextView) view.findViewById(R.id.item_item_address);
            holder.r1 = (RelativeLayout) view.findViewById(R.id.item_r1);
            holder.r2 = (RelativeLayout) view.findViewById(R.id.item_r2);
            holder.collection = (ImageView) view.findViewById(R.id.item_item_collect);
            holder.collect_tv= (TextView) view.findViewById(R.id.item_item_collect_tv);
            view.setTag(holder);

        } else
            holder = (ViewHolder) view.getTag();
        holder.tv.setText(bean.getData().getItems().get(i).getIntro());
        holder.price.setText(bean.getData().getItems().get(i).getPrice() + bean.getData().getItems().get(i).getPrice_unit());
        holder.address.setText(bean.getData().getItems().get(i).getDomain());
        holder.r1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,WebviewActivity.class);
                intent.putExtra("url",bean.getData().getItems().get(n).getCps_url());
                context.startActivity(intent);
            }
        });
        holder.r2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //收藏
                    if(holder.collect_tv.getText().equals("收藏")) {
                        holder.collection.setImageResource(R.drawable.ic_overseasshopping_collection);
                        holder.collect_tv.setText("已收藏");
                        Toast.makeText(context, "已收藏", Toast.LENGTH_SHORT).show();


                    }
                    else {
                        holder.collection.setImageResource(R.drawable.ic_overseasshopping_nocollection);
                        holder.collect_tv.setText("收藏");
                        Toast.makeText(context, "取消收藏", Toast.LENGTH_SHORT).show();
                    }

            }
        });


        //饱和度
//        ColorMatrix matrix = new ColorMatrix();
//        matrix.setSaturation(0);
//        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
//        holder.iv.setColorFilter(filter);
        Picasso.with(context).load(bean.getData().getItems().get(i).getImg_url()).into(holder.iv);
        return view;
    }

    class ViewHolder {
        private ImageView iv;
        private TextView tv;
        private ImageView collection;
        private TextView price, address,collect_tv;
        private RelativeLayout r1, r2;


    }


}
